import java.io.File;
import java.io.IOException;
import java.util.List;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.api.errors.UnmergedPathsException;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

public class GitRunner {
	public void p(String s) {
		System.out.println(s);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File f = new File("c:/repo/git/clone/bare");
		Git git = null;
		try {
			git = Git.init().setDirectory(f).call();
		} catch (GitAPIException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("test ---------------");
		/*
		 * for (RevCommit commit : git.log().call()) {
		 * System.out.println(commit.getAuthorIdent().getName());
		 * System.out.println(commit.getAuthorIdent().getWhen());
		 * System.out.println(commit.getFullMessage()); }
		 */

		try {
			git.add().addFilepattern("*").call();
		} catch (NoFilepatternException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GitAPIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			git.commit().setMessage("Test 1233").call();
		} catch (NoHeadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoMessageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnmergedPathsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConcurrentRefUpdateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WrongRepositoryStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GitAPIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			System.out.println("---- Test dif ----");
			ObjectId b1 = git.getRepository().resolve("HEAD^{tree}");
			ObjectId b2 = git.getRepository().resolve(
					"9559c4da0900910fbccd1e0f03a2513aa9aee84c^{tree}");

			ObjectReader reader = git.getRepository().newObjectReader();

			CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
			oldTreeIter.reset(reader, b1);
			CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
			newTreeIter.reset(reader, b2);

			List<DiffEntry> diffs = git.diff().setNewTree(newTreeIter)
					.setOldTree(oldTreeIter).call();

			for (DiffEntry de : diffs) {
				System.out.println(de.getNewPath() + "=>" + de.getOldPath());

			}

			Status status = git.status().call();
			for (String s : status.getAdded()) {
				System.out.println("Add: " + s);
			}

			for (String s : status.getChanged()) {
				System.out.println("Changed: " + s);
			}

			for (String s : status.getConflicting()) {
				System.out.println("Conflicting: " + s);
			}

			for (String s : status.getMissing()) {
				System.out.println("Missing: " + s);
			}

			for (String s : status.getModified()) {
				System.out.println("Modified:" + s);
			}

			for (String s : status.getRemoved()) {
				System.out.println("Removed: " + s);
			}

			for (String s : status.getUntracked()) {
				System.out.println("Untracked: " + s);
			}

			for (Ref r : git.branchList().call()) {
				System.out.println("ref: " + r.getName() + " leaf "
						+ r.getLeaf().getName() + " target "
						+ r.getTarget().getName());

			}

		} catch (AmbiguousObjectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GitAPIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
